<?php
// +----------------------------------------------------------------------
// | MC PHP[基于ThinkPHP6.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://www.mingchangruanjian.com
// +----------------------------------------------------------------------
// | Author jokerfei [ jokerfei377@gmail.com ]
// +----------------------------------------------------------------------
// | 铭畅软件 技术部 出品
// +----------------------------------------------------------------------

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\admin\model\user\User as UserModel;
use app\common\model\Api;
use app\common\service\ApiReturn;
use app\common\service\Str;
use think\facade\Cache;
use think\facade\Db;
use app\common\model\LogSms;
use app\admin\model\user\UnsetApply;
use app\admin\model\user\UnsetType;

/**
 * 用户接口
 * @package app\api\controller\v1
 */
class User extends Base
{

    /**
     * @Author: Bryant
     * @Date: 2023-07-19 09:32:56
     * @method post
     * @title 三方登录接口
     */
    public function thirdLogin($data = [], $user = [])
    {
        $user_third = Db::name('user_third')->where('type', $data['type'])->where('openid', $data['openid'])->find();
        if (!$user_third) {
            return ApiReturn::r(-999, null, '账号未注册');
        }
        $user = Db::name('user')->where('id', $user_third['user_id'])->find();
        if (!$user) {
            return ApiReturn::r(-999, null, '账号不存在');
        }
        if (!$user['status']) {
            return ApiReturn::r(0, null, '此用户被禁用或已注销，请联系客服咨询');
        }
        //获取登录需要返回的信息
        $userinfo = $this->get_login_info($user);
        return ApiReturn::r(1, compact('userinfo'), '登录成功'); //返回给客户端token信息
    }

    //登录凭证校验
    public function getCode2Session($data, $user)
    {
        try {
            $result = addons_action('WeChat', 'AuthCode', 'code2Session', [$data['code']]);
        } catch (\Exception $e) {
            return ApiReturn::r(0, [], $e->getMessage());
        }
        if(!$result){
           return ApiReturn::r(0, [], '请求失败');
        }

        if (isset($result['errcode']) && $result['errcode'] != 0) {
            return ApiReturn::r(0, [], $result['errmsg']);
        }
        $openid = $result['openid'];

        $user_third = Db::name('user_third')
        ->where('type','wx_xcx')
        ->where('openid',$result['openid'])
        ->find();

        if(!$user_third){
            return ApiReturn::r(-999, ['openid' => $openid], '账号未注册');
        }

        $user = UserModel::find($user_third['user_id']);
        if ($user && (!$user['status'] || $user['is_del'] == 1)) {
            return ApiReturn::r(0, null, '此用户被注销或禁用，请联系客服咨询');
        }
        // 获取登录需要返回的信息
        $userinfo = $this->get_login_info($user);
        return ApiReturn::r(1, $userinfo, '登录成功');
    }

    //微信登录绑定手机号
    public function bindMiniMobile($data, $user)
    {
        try {
            $result = addons_action('WeChat', 'AuthCode', 'getPhoneNumber', [$data['code']]);
        } catch (\Exception $e) {
            return ApiReturn::r(0, [], $e->getMessage());
        }

        if(!$result){
           return ApiReturn::r(0, [], '请求失败');
        }

        if (isset($result['errcode']) && $result['errcode'] != 0) {
            return ApiReturn::r(0, [], $result['errmsg']);
        }
        $openid = $data['openid'];
        $phone_info = $result["phone_info"];
        $mobile = $phone_info['purePhoneNumber'];
        $nickName = $data['nickName'] ?? '';
        $avatarUrl = $data['avatarUrl'] ?? '';

        $uinfo = UserModel::getByMobile($mobile);

        if (!$uinfo) {
            $UserModel = new UserModel();
            $regData = [
                'invite_code' => $data['invite_code'] ?? '',
                'password' => $data['password'] ?? 'abc123456',
                'mobile' => $mobile,
                'openid_type' => 'wx_xcx',
                'openid' => $data['openid'],
                'unionid' => '',
            ];
            $regData['user_nickname'] = $nickName;
            $regData['avatar'] = $avatarUrl;
            // 启动事务
            Db::startTrans();
            try {
                $user_id = $UserModel->get_reg_data($regData);
                $uinfo = $UserModel->find($user_id);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ApiReturn::r(0, null, $e->getMessage());
            }
        }

        $user_third = Db::name('user_third')
        ->where('user_id', $uinfo->id)
        ->where('type','wx_xcx')
        ->where('openid',$openid)
        ->find();

        if (!$user_third) {
            Db::name('user_third')->insert([
                    'user_id' => $uinfo->id,
                    'type' => 'wx_xcx',
                    'openid' => $openid,
                ]);
            if ($nickName) {
                $uinfo->user_nickname = $nickName;
            }
            if ($avatarUrl) {
                $uinfo->avatar = $avatarUrl;
            }
            $uinfo->save();
        }

        // 获取登录需要返回的信息
        $userinfo = $this->get_login_info($uinfo);

        return ApiReturn::r(1, $userinfo, '登录成功'); //返回给客户端token信息
    }


    /**
     * @Author: Bryant
     * @Date: 2023-06-12 14:23:57
     * @method post
     * @title 手机号密码登录
     */
    public function userMobilePasswordLogin(array $data)
    {
        //手机号登录
        $map = [];
        $map['mobile'] = $data['mobile'];
        $map['user_type'] = $data['user_type'];
        $user = UserModel::where($map)->field('id,user_nickname,password,status,mobile')->find();
        if (!$user) {
            return ApiReturn::r(-999, ['mobile' => $map['mobile']], '此用户不存在，请先注册！');
        } else {
            if (password($data['password']) != $user['password']) {
                return ApiReturn::r(0, null, '账号或者密码错误！');
            }
            if (!$user['status']) {
                return ApiReturn::r(0, null, '此用户被禁用，请联系客服咨询');
            }
            unset($user['password']);
            //获取登录需要返回的信息
            $jsonList = $this->get_login_info($user);
            return ApiReturn::r(1, ['userinfo' => $jsonList], '登录成功'); //返回给客户端token信息
        }
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-12 14:25:47
     * @method post
     * @title 手机号验证码登录
     */
    public function userMobileSmsLogin(array $data)
    {
        $map = [];
        $map['mobile'] = $data['mobile'];
        $map['user_type'] = $data['user_type'];
        $user = UserModel::where($map)->find();
        if (!$user) {
            return ApiReturn::r(-999, ['mobile' => $map['mobile']], '没有此用户，请注册后登录');
        }

        if (!LogSms::verifyCode($data['code'], $data['mobile'], $data['type'])) {
            return ApiReturn::r(0, null, '登录失败,验证码无效或已过期！');
        }
        if (!$user['status']) {
            return ApiReturn::r(0, null, '此用户被禁用，请联系客服咨询');
        }

        if ($user) {
            //获取登录需要返回的信息
            $jsonList = $this->get_login_info($user);
            return ApiReturn::r(1, ['userinfo' => $jsonList], '登录成功'); //返回给客户端token信息
        } else {
            return ApiReturn::r(-999, null, '该用户不存在');
        }
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-12 14:30:15
     * @method post
     * @title 生成登录需要的信息
     */
    public function get_login_info($user)
    {
        // 请求接口的token
        $exp_time1 = 2592000; //token过期时间,这里设置2个小时
        $scopes1 = 'role_access'; //token标识，请求接口的token
        $access_token = Api::createToken($user, $exp_time1, $scopes1);
        $uuid = Str::uuid();
        Cache::tag('user')->set('user_token_' . $uuid, $access_token);

        return [
            'user_token' => $uuid,
            'id' => $user['id'],
            'user_nickname' => $user['user_nickname'],
            'avatar' => $user['avatar'],
            'birthday' => $user['birthday'] ? date('Y-m-d', $user['birthday']) : '',
            'phone' => preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $user['mobile']),
            'mobile' => $user['mobile'],
            'email' => $user['email'],
            'gender' => $user['gender'],
            'invite_code' => $user['invite_code'],
        ];
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-10-31 09:51:11 
     * @method post 
     * @title 手机号一键登录 
     */
    public function oneKeyLogin($data = [], $user = [])
    {
        if($data['sign'] != md5($data['mobile'].'qianchengdaijia')){
            return ApiReturn::r(0,[],'参数错误');
        }
        // 启动事务
        Db::startTrans();
        try {
            $user = UserModel::where('mobile', $data['mobile'])->where('user_type', $data['user_type'])->find();
            if(!$user){
                $UserModel = new UserModel();
                $data['password'] = password($data['mobile']);
                $data['email'] = '';
                $data['user_type'] = $data['user_type'];
                $data['avatar'] = sysconfig('user','default_avatar', '');
                $data['user_nickname'] = '';
                $data['email'] = '';
                $data['email'] = '';
                $user = $UserModel->getRegData($data);
                if (!$user) {
                    throw new \Exception(lang('user.register_fail'));
                }
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $msg = $e->getMessage();
            return ApiReturn::r(0, null, $msg);
        }

        // 获取登录需要返回的信息
        $jsonList = $this->get_login_info($user);
        return ApiReturn::r(1, ['userinfo' => $jsonList], lang('user.register_successful'));
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-07 16:03:47
     * @method post
     * @title 用户注册
     */
    public function registerUser($data = [], $user = [])
    {
        $mobile_count = UserModel::where('mobile', $data['mobile'])->where('user_type', $data['user_type'])->count();
        if ($mobile_count > 0) {
            return ApiReturn::r(0, null, '手机号码已存在，无法注册');
        }
        if (!LogSms::verifyCode($data['code'], $data['mobile'], $data['type'])) {
            return ApiReturn::r(0, null, '验证码错误');
        }
        // 启动事务
        Db::startTrans();
        try {
            $UserModel = new UserModel();
            if (!isset($data['user_nickname'])) {
                $data['user_nickname'] = '';
            }
            $user_id = $UserModel->get_reg_data($data);
            if (!$user_id) {
                throw new \Exception('注册失败');
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $msg = $e->getMessage();
            return ApiReturn::r(0, null, $msg);
        }

        $user = UserModel::where('id', $user_id)->field('id,user_nickname,status,mobile')->find();
        // 获取登录需要返回的信息
        $jsonList = $this->get_login_info($user);
        //用户注册后
        \think\facade\Event::trigger('UserCreateAfter', $user);
        return ApiReturn::r(1, ['userinfo' => $jsonList], '注册成功');
    }


    /**
     * @Author: Bryant
     * @Date: 2023-06-12 15:40:46
     * @method post
     * @title 修改会员个人资料
     */
    public function editUserInfo($data = [], $user = [])
    {
        if (!$user) {
            return ApiReturn::r(1, null, '登录状态已失效，请重新登录');
        }

        $data['update_time'] = time();
        if (isset($data['birthday'])) {
            $data['birthday'] = strtotime($data['birthday']);
        }
        // 启动事务
        Db::startTrans();
        try {
            $result = UserModel::where('id', $user['id'])->update($data);
            if (!$result) {
                throw new \Exception('更新会员信息失败');
            }
            Db::commit();
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return ApiReturn::r(0, null, $e->getMessage());
        }
        $user_data = UserModel::where('id', $user['id'])->find();
        $jsonList = $this->get_login_info($user_data);
        return ApiReturn::r(1, ['userinfo' => $jsonList], '操作成功');
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-12 16:03:57
     * @method post
     * @title 更换绑定的手机号
     */
    public function changeMobile($data = [], $user = [])
    {
        $res = UserModel::where(['mobile' => $data['mobile']])->count();
        if ($res) {
            return ApiReturn::r(0, null, '此手机号已存在，请更换手机号');
        }
        if (!LogSms::verifyCode($data['code'], $data['mobile'], $data['type'])) {
            return ApiReturn::r(0, null, '验证码错误');
        }
        // 启动事务
        Db::startTrans();
        try {
            $res1 = UserModel::where('id', $user['id'])->update(['mobile' => $data['mobile']]);
            if (!$res1) {
                throw new \Exception('绑定失败');
            }
            Db::commit();
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return ApiReturn::r(0, null, $e->getMessage());
        }
        return ApiReturn::r(1, ['userinfo' => ['mobile' => $data['mobile']]], '更换手机号成功');
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-12 16:11:52
     * @method post
     * @title 更新用户表中用户的设备client_id
     */
    public function setClientId($data, $user)
    {
        $result = UserModel::where('id', $user['id'])->update(['client_id' => $data['client_id']]);
        if ($result) {
            return ApiReturn::r(1, null, '设置成功');
        }
        return ApiReturn::r(0, null, '操作失败');
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-12 15:33:12
     * @method post
     * @title 忘记密码
     */
    public function forgetPassword($data = [], $user = [])
    {
        $map = [];
        $map['mobile'] = $data['mobile'];
        //自定义信息，不要定义敏感信息
        $user = UserModel::where($map)->find();
        if (!$user) {
            return ApiReturn::r(-999, null, '没有此用户，请注册后登录');
        }
        if (!LogSms::verifyCode($data['code'], $data['mobile'], $data['type'])) {
            return ApiReturn::r(0, null, '验证码错误');
        }
        // 启动事务
        Db::startTrans();
        try {
            $password = password($data['password']);
            $result1 = UserModel::where('mobile', $data['mobile'])->update(['password' => $password]);
            if (!$result1) {
                throw new \Exception('重置密码失败');
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $msg = $e->getMessage();
            return ApiReturn::r(0, null, $msg);
        }

        return ApiReturn::r(1, null, '重置密码成功');
    }

    /**
     * 旧密码修改密码
     * @param array $data [description]
     * @param array $user [description]
     * @return [type]           [description]
     * @author mitu
     * @date   2023-11-18
     */
    public function resetPwdByOldPassword($data = [], $user = [])
    {
        if ($data['password'] != $data['repeat_password']) {
            return ApiReturn::r(0, null, '两次密码不一致');
        }
        $user = UserModel::find($user['id']);
        $password = password($data['old_password']);
        if ($password != $user['password']) {
            return ApiReturn::r(0, null, '旧密码错误');
        }
        $password = password($data['password']);
        $user->password = $password;
        $user->save();
        return ApiReturn::r(1, null, '设置成功');
    }

    /**
     * 验证旧支付密码
     * @param array $data [description]
     * @param array $user [description]
     * @return [type]           [description]
     * @author mitu
     * @date   2023-11-18
     */
    public function isPayPwdNull($data = [], $user = [])
    {
        $user = UserModel::find($user['id']);
        if (!$user['pay_password']) {
            return ApiReturn::r(0, null, '未设置');
        }
        return ApiReturn::r(1, null, '已设置');
    }

    /**
     * 验证旧支付密码
     * @param array $data [description]
     * @param array $user [description]
     * @return [type]           [description]
     * @author mitu
     * @date   2023-11-18
     */
    public function verifyPayPwd($data = [], $user = [])
    {
        $user = UserModel::find($user['id']);
        $password = password($data['password']);
        if ($password != $user['pay_password']) {
            return ApiReturn::r(0, null, '密码错误');
        }
        return ApiReturn::r(1, null, '验证成功');
    }

    /**
     * 旧支付密码修改密码
     * @param array $data [description]
     * @param array $user [description]
     * @return [type]           [description]
     * @author mitu
     * @date   2023-11-18
     */
    public function resetPayPwdByOldPassword($data = [], $user = [])
    {
        if ($data['password'] != $data['repeat_password']) {
            return ApiReturn::r(0, null, '两次密码不一致');
        }
        $user = UserModel::find($user['id']);
        if ($user['pay_password']) {
            if (empty($data['old_password']) ) {
                return ApiReturn::r(0, null, '请填写旧支付密码');
            }
            $password = password($data['old_password']);
            if ($password != $user['pay_password']) {
                return ApiReturn::r(0, null, '旧密码错误');
            }
        }
        $password = password($data['password']);
        $user->pay_password = $password;
        $user->save();
        return ApiReturn::r(1, null, '设置成功');
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-12 16:39:54
     * @method post
     * @title 设置余额支付密码
     */
    public function setPayPwd($data = [], $user = [])
    {
        $map = [];
        $map['mobile'] = $data['mobile'];
        //自定义信息，不要定义敏感信息
        $user = UserModel::where($map)->find();
        if (!$user) {
            return ApiReturn::r(-999, null, '没有此用户，请注册后登录');
        }
        if (!LogSms::verifyCode($data['code'], $data['mobile'], $data['type'])) {
            return ApiReturn::r(0, null, '验证码错误');
        }
        // 启动事务
        Db::startTrans();
        try {
            $password = password($data['pay_password']);
            $result1 = UserModel::where('id', $user['id'])->update(['pay_password' => $password]);
            if (!$result1) {
                throw new \Exception('设置支付密码失败');
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $msg = $e->getMessage();
            return ApiReturn::r(0, null, $msg);
        }
        return ApiReturn::r(1, null, '设置支付密码成功');
    }

    /**
     * @Author: Bryant
     * @Date: 2023-06-12 18:23:48
     * @method post
     * @title 获取用户信息
     */
    public function getUserInfo($data = [], $user = [])
    {
        $user = Db::name('user')
            ->alias('u')
            ->join('user_assets a', 'u.id=a.user_id')
            ->where('u.id', $user['id'])
            ->field('u.*')
            ->field('a.user_money,a.score')
            ->find();
        $params = [
            'id' => $user['id'],
            'user_nickname' => $user['user_nickname'],
            'avatar' => $user['avatar'],
            'birthday' => $user['birthday'] ? date('Y-m-d', $user['birthday']) : '',
            'phone' => preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $user['mobile']),
            'mobile' => $user['mobile'],
            'email' => $user['email'],
            'gender' => $user['gender'],
            'invite_code' => $user['invite_code'],
            'user_money' => $user['user_money'],
            'score' => $user['score'],
            'user_name' => $user['user_name'],
            'card_no' => $user['card_no'],
            'address' => $user['address'],
            'start_price' => $user['start_price'],
            'images' => explode('|',$user['images']),
            'image' => $user['image'],
            'talent' => explode(',',$user['talent']),
            'weight' => $user['weight'],
            'height' => $user['height'],
            'drving_age' => $user['drving_age'],
            'online_status' => $user['online_status'],
            'is_certify' => $user['is_certify'],
            'preference' =>  $user['preference'],
            'refresh_num' => $user['refresh_num'],
            'expire_time' => $user['expire_time'] ? date("Y-m-d H:i:s",$user['expire_time']) : '',
            'is_vip' => $user['is_vip']
        ];
        return ApiReturn::r(1, $params, '请求成功');
    }

    //注销类型
    public function unsetType($data = [], $user = [])
    {
        $list = UnsetType::where('status', 1)
            ->order('sort asc, id desc')
            ->select();
        return ApiReturn::r(1, $list, lang('common.successful'));
    }

    //注销申请
    public function unsetApply($data = [], $user = [])
    {
        $user = UserModel::find($user['id']);
        $apply = UnsetApply::where('user_id', $user->id)->find();
        if (!$apply) {
            $data['user_id'] = $user->id;
            $data['mobile'] = $user->mobile;
            $data['old_user_nickname'] = $user->user_nickname;
            $data['old_avatar'] = $user->avatar;
            UnsetApply::create($data);
        } else {
            if ($apply->status == -1) {
                $apply->status = 0;
                $apply->save();
            }
        }
        $user->status = 0;
        $user->save();
        return ApiReturn::r(1, null, '请求成功');
    }

    //注销信息
    public function unsetInfo($data = [], $user = [])
    {
        $apply = UnsetApply::where('user_id', $user['id'])->find();
        if (!$apply) {
            return ApiReturn::r(0, null, lang('common.not_applied'));
        }
        return ApiReturn::r(1, $apply, lang('common.successful'));
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-12-16 11:12:02 
     * @method post 
     * @title 添加认证 
     */
    public function addCertify($data,$user)
    {
        $res = \app\admin\model\UserCertify::where('user_id',$user['id'])->where('status',1)->find();
        if($res){
            return ApiReturn::r(1,[],'已认证');
        }
        $res = certify($data['user_name'],$data['card_no']);
        if(!$res->success || $res->data->result){
            $data['update_time'] = time();
            $data['reason'] =  '身份证不一致';
            return ApiReturn::r(0,[],'认证失败，原因：身份证不一致');
        }
        $data['user_id'] = $user['id'];
        $data['status'] = 1;
        \app\admin\model\UserCertify::create($data);
        \app\admin\model\user\User::where('id',$user['id'])
        ->update([
            'is_certify' => 1,
            'user_name' => $data['user_name'],
            'card_no' => $data['card_no'],
        ]);
        return ApiReturn::r(1,[],'ok');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-12-16 11:48:47 
     * @method post 
     * @title 获取司机 
     */
    public function getDriver($data,$user)
    {
        $user_id = $user['id'] ?? 0;
        if($data['refresh']){
            $user = \app\admin\model\user\User::where('id',$user_id)->find();
            if($user){
                if(!$user['is_vip']){
                    if($user['refresh_num'] > 1){
                        \app\admin\model\user\User::where('id',$user['id'])->dec('refresh_num')->update();
                    }else{
                        return ApiReturn::r(0,[],'没有刷新次数了');
                    }
                }
            }else{
                return ApiReturn::r(0,[],'没有刷新次数了');
            }
            
        }
        $field="round(st_distance_sphere(point(".$data["lng"].",".$data["lat"]."),point(lng,lat))) distance";
        $list = \app\admin\model\user\User::where('user_type',2)
        ->field($field)
        ->field('id,image,user_nickname,user_level,address,lng,lat,start_price,talent,weight,height,drving_age,online_status')
        ->where('gender',$data['gender'])
        ->where('online_status',1)
        ->paginate(sysconfig('user','driver_num'))
        ->each(function($item){
            $item['talent'] = explode(',',$item['talent']);
            if($item['distance'] < 1000){
                $item['distance'] = $item['distance'] . 'm';
            }else{
                $item['distance'] = bcdiv($item['distance'],1000,2) . 'Km';
            }
            return $item;
        });
        return ApiReturn::r(1,$list,'ok');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-12-16 12:12:55 
     * @method post 
     * @title 司机详情 
     */
    public function getDriverInfo($data,$user)
    {
        $field="round(st_distance_sphere(point(".$data["lng"].",".$data["lat"]."),point(lng,lat))) distance";
        $item = \app\admin\model\user\User::where('id',$data['id'])
        ->where('user_type',2)
        ->field($field)
        ->field('id,image,images,user_nickname,user_level,address,lng,lat,start_price,talent,weight,height,drving_age,online_status')
        ->find();
        if(!$item){
            return ApiReturn::r(0,[],'司机不存在');
        }
        $item['talent'] = explode(',',$item['talent']);
        $item['image'] = explode('|',$item['image']);
        if($item['distance'] < 1000){
            $item['distance'] = $item['distance'] . 'm';
        }else{
            $item['distance'] = bcdiv($item['distance'],1000,2) . 'Km';
        }
        return ApiReturn::r(1,$item,'ok');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-12-16 15:46:10 
     * @method post 
     * @title 充值规则 
     */
    public function chargeRule()
    {
        $list = \app\admin\model\UserRechargeRule::order('sort desc')
        ->where('status' ,1)
        ->select();
        return ApiReturn::r(1,$list,'ok');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-12-16 15:53:08 
     * @method post 
     * @title 会员等级 
     */
    public function chargeLevel()
    {
        $list = \app\admin\model\UserLevel::order('sort desc')
        ->where('status' ,1)
        ->select();
        return ApiReturn::r(1,$list,'ok');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-12-16 15:58:43 
     * @method post 
     * @title 会员权益 
     */
    public function interests()
    {
        $list = \app\admin\model\UserLevelInterests::where('status',1)
        ->order('sort desc')
        ->select();
        return ApiReturn::r(1,$list,'ok');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2024-12-16 16:36:20 
     * @method post 
     * @title 获取余额记录 
     */
    public function getMoneyLog($data,$user)
    {
        $where[] = ['user_id', '=', $user['id']];
        if($data['start_time']){
            $where[] = ['create_time','>',strtotime($data['start_time'].' 00:00:00')];
        }
        if($data['end_time']){
            $where[] = ['create_time','<',strtotime($data['end_time'].' 23:59:59')];
        }
        if($data['change_type'] == 1){//收入
            $where[] = ['change_money','>',0];
        }elseif($data['change_type'] == 2){//支出
            $where[] = ['change_money','<',0];
        }
        $list = \app\admin\model\user\MoneyLog::where($where)
        ->order('create_time', 'desc')->paginate();
        return ApiReturn::r(1, $list, lang('common.successful'));
    }
}
